package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.common.db.LongResultSetReader;
import com.singfan.common.util.StringUtil;
import com.singfan.core.dao.database.IShopHairDao;
import com.singfan.core.dao.database.model.ShopHair;
import com.singfan.core.dao.database.model.constant.FilterType;
import com.singfan.core.dao.database.model.constant.HairServiceType;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Author: hansonghe
 * Date: 2015-08-19 12:12
 * Desc:
 */
@Repository
public class ShopHairDaoImpl extends DaoBaseGeneric<ShopHair> implements IShopHairDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public void create(ShopHair shopHair) {
        insert(null, shopHair);
    }

    @Override
    public List<ShopHair> selectShopHairList(int serviceType, int listOrderType, int cityId, int businessCircleId, int pageIndex, int pageSize) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE ";
        List<Object> paramList = new ArrayList<>();
        if (businessCircleId > 0){
            sql += "business_circle_id=? ";
            paramList.add(businessCircleId);
        }
        else {
            sql += "city_id=? ";
            paramList.add(cityId);
        }

        if (serviceType == HairServiceType.HAIR_CUT){
            sql += "AND hair_cut=?";
            paramList.add(true);
        }
        else if (serviceType == HairServiceType.HAIR_PERM){
            sql += "AND hair_perm=?";
            paramList.add(true);
        }
        else if (serviceType == HairServiceType.HAIR_DYE){
            sql += "AND hair_dye=?";
            paramList.add(true);
        }
        else if (serviceType == HairServiceType.HAIR_NURSE){
            sql += "AND hair_nurse=?";
            paramList.add(true);
        }
        else if (serviceType == HairServiceType.HAIR_MODELING){
            sql += "AND hair_modeling=?";
            paramList.add(true);
        }

        else if (serviceType == HairServiceType.HAIR_MAN_30){
            sql += "AND man_30=?";
            paramList.add(true);
        }
        else if (serviceType == HairServiceType.HAIR_MAN_60){
            sql += "AND man_60=?";
            paramList.add(true);
        }
        else if (serviceType == HairServiceType.HAIR_MAN_90){
            sql += "AND man_90=?";
            paramList.add(true);
        }

        if (listOrderType == FilterType.COMPLEX){
            sql += " ORDER BY order_count DESC";
        }
        else if (listOrderType == FilterType.HONOR){
            sql += " ORDER BY honor DESC";
        }

        sql += " limit ?,?";

        paramList.add(pageIndex * pageSize);
        paramList.add(pageSize);

        return selectList(null, sql, paramList);
    }

    @Override
    public List<ShopHair> selectShopHairList(List<Long> shopIdList) {
        int shopCount = shopIdList.size();
        List<ShopHair> shopHairList = new ArrayList<>(shopCount);
        List<Object> paramList = new ArrayList<>();
        if (shopCount > 0){
            String sql = "SELECT * FROM " + getTableName(null) + " WHERE shop_id in (";
            for (int i=0; i<shopCount; i++){
                if (i == 0){
                    sql += "?";
                }
                else {
                    sql += ",?";
                }

                paramList.add(shopIdList.get(i));
            }
            sql += ")";

            shopHairList = selectList(null, sql, paramList);
        }

        return shopHairList;
    }

    @Override
    public ShopHair selectShopHair(long shopId) {
        return selectByPK(null, shopId);
    }

    @Override
    public void updateCollectCount(long shopId, boolean isCollected) {
        String sql;
        if (isCollected){
            sql = "UPDATE " + getTableName(shopId) + " SET collect_count = collect_count+1 WHERE shop_id=?";
        }
        else {
            sql = "UPDATE " + getTableName(shopId) + " SET collect_count = collect_count-1 WHERE shop_id=?";
        }
        execute(shopId, sql, shopId);
    }

    @Override
    public void updateComment(long shopId, int commentCount, int commentScore, int honor) {
        String sql = "UPDATE " + getTableName(shopId) + " SET comment_count=?, comment_score=?, honor=? WHERE shop_id=?";
        execute(shopId, sql, commentCount, commentScore, honor, shopId);
    }

    @Override
    public List<ShopHair> search(String searchText, int pageIndex, int pageSize) {
        if (StringUtil.isNullOrEmpty(searchText) || StringUtil.isNullOrEmpty(searchText.trim())){
            String sql = "SELECT * FROM " + getTableName(null) + " limit ?,?";
            return selectList(null, sql, pageIndex * pageSize, pageSize);
        }
        else {
            String sql = "SELECT * FROM " + getTableName(null) + " WHERE master_shop_name like '%"+searchText.trim()+"%' limit ?,?";
            return selectList(null, sql, pageIndex * pageSize, pageSize);
        }
    }

    @Override
    public List<Long> selectShopIdList() {
        String sql = "SELECT shop_id FROM " + getTableName(null);
        return (List<Long>)executeQuery(null, sql, null, new LongResultSetReader());
    }

    @Override
    public List<ShopHair> selectSlaveShopList(int masterShopId, int pageIndex, int pageSize) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE master_shop_id=? limit ?,?";
        return selectList(null, sql, masterShopId, pageIndex*pageSize, pageSize);
    }

    @Override
    public void increaseBarberCount(long shopId) {
        String sql = "UPDATE " + getTableName(shopId) + " SET barber_count = barber_count+1 WHERE shop_id=?";
        execute(shopId, sql, shopId);
    }

    @Override
    public void supportServiceType(long shopId, int serviceType){
        if (serviceType == HairServiceType.HAIR_CUT){
            String sql = "UPDATE " + getTableName(shopId) + " SET hair_cut=? WHERE shop_id=?";
            execute(null, sql, true, shopId);
        }
        else if (serviceType == HairServiceType.HAIR_PERM){
            String sql = "UPDATE " + getTableName(shopId) + " SET hair_perm=? WHERE shop_id=?";
            execute(null, sql, true, shopId);
        }
        else if (serviceType == HairServiceType.HAIR_DYE){
            String sql = "UPDATE " + getTableName(shopId) + " SET hair_dye=? WHERE shop_id=?";
            execute(null, sql, true, shopId);
        }
        else if (serviceType == HairServiceType.HAIR_NURSE){
            String sql = "UPDATE " + getTableName(shopId) + " SET hair_nurse=? WHERE shop_id=?";
            execute(null, sql, true, shopId);
        }
        else if (serviceType == HairServiceType.HAIR_MODELING){
            String sql = "UPDATE " + getTableName(shopId) + " SET hair_modeling=? WHERE shop_id=?";
            execute(null, sql, true, shopId);
        }

        else if (serviceType == HairServiceType.HAIR_MAN_30){
            String sql = "UPDATE " + getTableName(shopId) + " SET man_30=? WHERE shop_id=?";
            execute(null, sql, true, shopId);
        }
        else if (serviceType == HairServiceType.HAIR_MAN_60){
            String sql = "UPDATE " + getTableName(shopId) + " SET man_60=? WHERE shop_id=?";
            execute(null, sql, true, shopId);
        }
        else if (serviceType == HairServiceType.HAIR_MAN_90){
            String sql = "UPDATE " + getTableName(shopId) + " SET man_90=? WHERE shop_id=?";
            execute(null, sql, true, shopId);
        }
    }

    @Override
    public void update(ShopHair shopHair) {
        updateByPK(null, shopHair);
    }

    @Override
    public void updateStatus(long shopId, int status) {
        String sql = "UPDATE " + getTableName(null) + " SET status=? WHERE shop_id=?";
        execute(null, sql, status, shopId);
    }
}
